package msgobj

import (
	"encoding/json"
	"fmt"
	"time"
)

type AppReportPoint struct {
	Key  string `json:"key"`
	Val  any    `json:"val"`
	Time int64  `json:"time"`
}

func NewAppReportPoint(k string, v any) *AppReportPoint {
	return &AppReportPoint{
		Key:  k,
		Val:  v,
		Time: time.Now().UnixMilli(),
	}
}

type AppReportDev struct {
	Name   string            `json:"name"`
	Points []*AppReportPoint `json:"points"`
}

func NewAppReportDev(n string, p ...*AppReportPoint) *AppReportDev {
	return &AppReportDev{
		Name:   n,
		Points: append([]*AppReportPoint{}, p...),
	}
}

type AppReportMsg struct {
	Tick int             `json:"tick"`
	Time int64           `json:"time"`
	Src  string          `json:"src"`
	Apps []*AppReportDev `json:"app"`
}

func NewAppReportMsg(s string, a ...*AppReportDev) *AppReportMsg {
	return &AppReportMsg{
		Tick: 1,
		Time: time.Now().UnixMilli(),
		Src:  s,
		Apps: append([]*AppReportDev{}, a...),
	}
}

func (a *AppReportMsg) ToJson() string {
	v, err := json.Marshal(a)
	if err != nil {
		fmt.Println("json marshal error, err is ", err)
		return ""
	}

	return string(v)
}
